#include "loginscreen.h"
#include "ui_loginscreen.h"
#include "client.h"
#include "global.h"
#include <QMessageBox>

loginScreen::loginScreen(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::loginScreen)
{
    ui->setupUi(this);

    this->setFixedSize(325,235);
}

loginScreen::~loginScreen()
{
    delete ui;
}


void loginScreen::on_btnQuit_clicked()
{
    QApplication::quit();
}

void loginScreen::on_btnLogin_clicked()
{
    if(ui->lineEdit->text().isEmpty())return;
    client = new Client();    // 如果本地IP获取成功，则可以进行广播IP和获取在线用户列表的功能了

    if(client->getValidIP()){
        client->setUserName(ui->lineEdit->text());
        client->broadcastInfo();
        if(client->connectServer() == false){
            QMessageBox::critical(this,tr("错误信息"),tr("无法连接服务器"));
            return;
        }
        client->sendLocalInfoToServer();
        client->show();
        this->close();
    }else{
        QMessageBox::critical(this,tr("错误信息"),tr("本地IP获取失败"));
    }
}
